package com.tedu.movie.controller;

import com.tedu.movie.pojo.ServerResult;
import com.tedu.movie.pojo.User;
import com.tedu.movie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin
@RestController
public class UserController {
    //取业务层对象
    @Autowired
    UserService userService;
    //@RequestMapping("/user/login")
    //public User login(User user){
    //   User dbUser=userService.login(user);
    //   return dbUser;
    // }
    @RequestMapping("/user/register")
    public ServerResult register(User user){
        boolean isSuccess = userService.register(user);
        ServerResult serverResult=null;
        if(isSuccess){
            serverResult=new ServerResult(0,"注册成功",null);

        }else{
            serverResult=new ServerResult(1,"注册失败",null);

        }
        return  serverResult;
    }


    @RequestMapping("/user/login")
    public ServerResult login(User user) {
        User dbUser = userService.login(user);
        if (ObjectUtils.isEmpty(dbUser)) {
            ServerResult serverResult = new ServerResult(1, "登陆失败", null);
            return serverResult;
        } else {
            dbUser.setPassword("");
            ServerResult serverResult = new ServerResult(0, "登陆成功", dbUser);
            return serverResult;
        }
    }
}
